package com.example.demo.controller;


import com.example.demo.entity.Option;
import com.example.demo.entity.teacher;
import com.example.demo.entity.userAdd;
import com.example.demo.entity.userSec;
import com.example.demo.model.result;
import com.example.demo.services.userLoginServices;
import com.example.demo.services.userSecServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/userSec")
public class userSecController {


    @Autowired
    private userSecServices userSecServices;

    @Autowired
    private userLoginServices userLoginServices;


    private Logger logger= LoggerFactory.getLogger(userSecController.class);

    @PostMapping("/update")
    public result update(userSec userSec,String name){
        Integer test=userLoginServices.updateLogin(name,userSec.getId());
        if(test==1){
            return result.success(userSecServices.updateUser(userSec));
        }else {
            return result.fail("修改姓名发生错误");
        }
    }


    @GetMapping("/getAll")
    public result getAll() {
        List<userAdd> userAdds=userSecServices.selectAll();
        List<userAdd> list = new ArrayList<>();
        for (userAdd userAdda : userAdds){
            if(userAdda.getTeacherId()==null){
                list.add(userAdda);
            }
        }
        if(list!=null){
            return result.success(list);
        }else{
            return result.fail("没有对应数据");
        }
    }

    @GetMapping("/getTeacher")
    public result getTeacher(){
        List<teacher> teachers=userSecServices.selectTeacher();
        List<Option> option=new ArrayList<Option>();
        for(teacher test:teachers){
            if(teachers!=null){
                Option option1=new Option();
                option1.setValue(String.valueOf(test.getId()));
                option1.setLabel(test.getName());
                option.add(option1);
            }
        }
        if(option!=null){
            return result.success(option);
        }else{
            return result.fail("没有老师数据");
        }
    }

    @PostMapping("/updateLeader")
    public result updateLeader(String userId,Integer teacherId){
        logger.info("修改{}辅导员老师信息成功",userId);
        return result.success(userSecServices.updateTeacher(userId, teacherId));
    }


}
